<template>
  <div class="bg-light">
    <!--标签区-->
    <div>
      <!--$event(selectTags)表示子组件传过来的数据，这是固定写法。
      赋值给note.noteTags-->
      <NoteTag @getTags="note.noteTags = $event"/>
    </div>
    <!--工具栏区域-->
    <el-affix :offset="71">
      <div id="editor-toolbar" class="shadow"></div>
      <div>
        <el-button @click="saveNote" class="border-0">保存</el-button>
      </div>
    </el-affix>
    <!--编辑区-->
    <div class="px-5 py-5">
      <!--标题区-->
      <div class="shadow-lg">
        <input
          type="text"
          v-model="note.noteName"
          class="note-name px-3 py-3"
          placeholder="请输入标题...."
        />
      </div>
      <!--内容输入区-->
      <div id="editor-container" class="note-content shadow-lg px-3 py-3"></div>
    </div>
  </div>
</template>

<script>
import Editor from "@/tools/editor.js";
import NoteTag from "./NoteTag.vue";
export default {
  name: "NoteAdd",
  components: {
    NoteTag,
  },

  data() {
    return {
      editor: null,
      note: {
        //要到后端的数据
        noteName: null,
        noteIntro: null,
        noteContent: null,
        noteTags: [],
      },
    };
  },
  mounted() {
    this.editor = new Editor();
    this.editor.init();
  },
  methods: {
    //保存笔记
    saveNote() {
      this.note.noteIntro = this.editor.getNoteIntro();
      this.note.noteContent = this.editor.getNoteContent();
      console.log(this.note);
      //发起网络请求
      this.$axios.post('/api/space/note/add',this.note)
      .then(response=>{
        console.log(response);
        let data = response.data
        if(data.code == 200){
          this.$router.push("/space/")
        }
        alert(data.msg)
      })
    },
  },
};
</script>

<style scoped>
/*作用在input标签*/
.note-name {
  width: 100%;
  border: none;
  outline: none;
  font-size: 3rem;
}

/*作用在内容输入区*/
.note-content {
  min-height: 900px;
  background-color: #fff;
}
</style>